import { AuthVO } from '@/services/auth.ts';
import { create } from 'zustand';

type UserStore = {
  user: AuthVO | null
  setUser(user: AuthVO | null): void
  modifyUser(user: Partial<AuthVO>): void
}

const useUserStore = create<UserStore>((set, get) => ({
  user: null,
  setUser(user) {
    if (user && !user.name) {
      user.name = `用户${user.phoneNumber}`;
    }
    set({
      user
    });
  },
  modifyUser(user) {
    const storeUser = get().user;
    if (!storeUser) return;

    set({
      user: {
        ...storeUser,
        ...user
      }
    });
  }
}));

export default useUserStore;

export function useUserInfo() {
  return useUserStore(state => state.user);
}

export function useHasLogin() {
  return !!useUserInfo();
}